package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @classname:DBHelper
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/30 21:36
 * @version: 0.1
 * @since:1.8
 */
public class DBHelper {

    private DBHelper(){}
    private static DataSource dataSource;
    static {
        //初始化数据源对象 并创建多个对象 DruidDataSource.
       //工长模式
        Properties properties = new Properties();
        try {
            properties.load(DBHelper.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            dataSource  = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getCon(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static DataSource getDataSource(){
        return dataSource;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(()->{
                for (int j = 0; j < 10; j++) {
                    System.out.println(Thread.currentThread().getName()+"--"+DBHelper.getCon());
                }
            }).start();

        }

    }




}
